package cn.itcast.web.action;

import cn.itcast.domain.transit.TransitInfo;
import cn.itcast.service.TransitService;
import cn.itcast.utils.ResponseUtils;
import cn.itcast.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
public class TransitAction extends BaseAction<TransitInfo> {
    @Autowired
    private TransitService transitService;

    // $.post("../../transit_create.action",{wayBillIds:wayBillIds},function (data)
    private String wayBillIds;

    public void setWayBillIds(String wayBillIds) {
        this.wayBillIds = wayBillIds;
    }
    @Action(value = "transit_create",results = {@Result(name = "success",type = "json")})
    public String create(){
        Map<String,Object> results=new HashMap<>();
        try {
            transitService.createTransits(wayBillIds);
            results.put("success",true);
            results.put("msg","开启中转配送成功");
        }catch (Exception e){
            e.printStackTrace();
            results.put("success",false);
            results.put("msg","开启中转配送失败");
        }
        ServletActionContext.getContext().getValueStack().push(results);
        return SUCCESS;
    }
    //   url: '../../transit_pageQuery.action',
    @Action(value = "transit_pageQuery")
    public String pageQuery(){
        Pageable pageable=new PageRequest(page-1,rows);
         Page<TransitInfo> pageData= transitService.findPageData(pageable);
        Map<String,Object>results=new HashMap<>();
        results.put("total",pageData.getTotalElements());
        results.put("rows",pageData.getContent());
        String data = JSONArray.toJSONString(results);
        ResponseUtils.responseWriter(data);
        System.out.println(data);
        return NONE;
    }
}
